﻿using System;
using System.Linq;
using System.Threading.Tasks;

namespace Athena.RouteAddress.Implementations
{
    public abstract class BaseRouteSelector : IRouteSelector
    {
        Task<Route> IRouteSelector.SelectAsync(ServiceRoute serviceRoute)
        {
            if (serviceRoute == null)
            {
                throw new ArgumentNullException(nameof(serviceRoute));
            }
            if (string.IsNullOrWhiteSpace(serviceRoute.CorrelationId) || serviceRoute.Routes == null) {
                throw new ArgumentException(nameof(serviceRoute));
            }
            var count = serviceRoute.Routes.Count();
            if (count == 0) {
                throw new ArgumentException(nameof(serviceRoute.Routes));
            }
            if (count == 1) {
                return Task.FromResult(serviceRoute.Routes.First());
            }

            return SelectAsync(serviceRoute);
        }

        protected abstract Task<Route> SelectAsync(ServiceRoute serviceRoute);
    }
}
